package cn.huihuo.weibo.service;


import cn.huihuo.weibo.bean.message.MessageBean;
import cn.huihuo.weibo.bean.message.MessageResultBean;
import cn.huihuo.weibo.module.Comment;
import cn.huihuo.weibo.module.Message;
import cn.huihuo.weibo.utils.TimeUtils;
import com.jfinal.plugin.activerecord.Page;

import java.util.ArrayList;

/**
 * Created by zhengjunbo on 2015/11/2.
 */
public class MessageService {
    public static final MessageService service = new MessageService();


    /**
     * 获取
     * @param pageNum
     * @param count
     * @param userId
     * @return
     */
    public MessageResultBean getMessage(Integer pageNum,Integer count,String userId){
        MessageResultBean messageResultBean = new MessageResultBean();

        Page<Message> messagePage = Message.dao.findProductCommentBuyProdId(pageNum, count, userId);
        messageResultBean.setTotalSize(String.valueOf(messagePage.getTotalPage()));
        messageResultBean.setMessages(new ArrayList<>());

        for(Message message: messagePage.getList()){
            MessageBean messageBean = new MessageBean();

            String type = message.getStr("type");
            if("prodComm".equals(type)){
                assembleProdComm(messageBean,message);
            }

            if("comm".equals(type)){
                assembleComm(messageBean,message);
            }

            //完成之后要统一更新下已读状态
            message.set("is_read",1).update();
            messageResultBean.getMessages().add(messageBean);
        }
        return messageResultBean;
    }

    private void assembleComm(MessageBean messageBean, Message message) {
        Integer replyId = message.getInt("reply_id");
        Comment comment = Comment.comment.findReplayCommByCommId(replyId);
        if(comment==null){
            return;
        }
        messageBean.setReplayId(String.valueOf(comment.getInt("replayId")));
        messageBean.setReplayAvatar(comment.getStr("avatar"));
        messageBean.setReplayTime(TimeUtils.getTime(message.getTimestamp("write_time").getTime()));
        messageBean.setReplayUserId(comment.getStr("userId"));
        messageBean.setReplayDescription("回复我的评论：");
        messageBean.setUgcUrl("/ugc/" + comment.getInt("ugcId"));
        messageBean.setReplayUrl("/user/" + comment.getStr("userId"));
        messageBean.setReplayName(comment.getStr("nickname"));
        messageBean.setReplayMessage(comment.getStr("myComment"));
        messageBean.setReplayId(""+comment.getInt("replayId"));
        messageBean.setUgcId("" + comment.getInt("ugcId"));

        messageBean.setMessageId(String.valueOf(message.getInt("id")));
        messageBean.setMessageType(message.getStr("type"));
        messageBean.setMyMessage(comment.getStr("replayComment"));

        messageBean.setIsRead("" + message.getInt("is_read"));
    }

    private void assembleProdComm(MessageBean messageBean, Message message) {
        Integer replyId = message.getInt("reply_id");
        Comment comment = Comment.comment.findProdCommByCommId(replyId);
        if(comment==null){
            return;
        }
        messageBean.setMessageType(message.getStr("type"));
        messageBean.setReplayMessage(comment.getStr("replayComment"));
        messageBean.setMyMessage(comment.getStr("myComment"));
        messageBean.setReplayName(comment.getStr("nickname"));
        messageBean.setReplayAvatar(comment.getStr("avatar"));
        messageBean.setUgcId("" + comment.getInt("ugcId"));
        messageBean.setReplayUserId(comment.getStr("userId"));
        messageBean.setIsRead(String.valueOf(message.getInt("is_read")));
        messageBean.setMessageId(String.valueOf(message.getInt("id")));
        messageBean.setUgcUrl("/ugc/" + comment.getInt("ugcId"));
        messageBean.setReplayUrl("/user/" + comment.getStr("userId"));
        messageBean.setReplayDescription(getDescription(comment.getStr("ugcType")));
        messageBean.setReplayTime(TimeUtils.getTime(message.getTimestamp("write_time").getTime()));
        messageBean.setIsRead("" + message.getInt("is_read"));
        messageBean.setReplayId(String.valueOf(replyId));
    }

    private String getDescription(String ugcType) {
        if("2".equals(ugcType)){
            return  "评论我的讨论：";
        }

        if("3".equals(ugcType)){
            return "评论我的摘抄：";
        }

        if("4".equals(ugcType)){
            return "评论我的购买渠道：";
        }
        return "";
    }


    public String total(String userId){
        if(userId==null){
            return "";
        }
        Message unReadTotal = Message.dao.findUnReadTotal(userId);
        Long size = unReadTotal.getLong("size");
        if(size==0){
            return "";
        }
        if(size>9){
            return "9+";
        }
        return String.valueOf(size);
    }
}
